import { Service } from "egg";

export default class User extends Service {
    /**
     * 提交反馈
     * */
    public async submitFeedback() {
        const { ctx, app } = this;
        const { Joi } = app;
        let schema = Joi.object({
            content: Joi.string().max(500).required(),
            contact: Joi.string().max(50).required(),
            systemInfo: Joi.string().max(150),
            ua: Joi.string().max(200),
            location: Joi.string().max(120).allow('')
        });
        let { error } = ctx.validate(schema, ctx.request.body);
        if (error) {
            ctx.status = 403;
            return { error: error.message };
        };
        app.mysql.get(app.dbName).insert('feedback', ctx.request.body)
        return 'success'
    }
    /**
     * 提交反馈
     * */
    public async saveUserByUuid() {
        const { ctx, app } = this;
        // console.log(ctx.request.body);

        const { Joi } = app;
        let schema = Joi.object({
            imei: Joi.string().max(50).required(),
            uuid: Joi.string().allow(''),
            imsi: Joi.string().allow(''),
            vendor: Joi.string().allow(''),
            model: Joi.string().allow(''),
        });
        let { error } = ctx.validate(schema, ctx.request.body);
        if (error) {
            ctx.status = 403;
            return { error: error.message };
        };
        let list = await app.mysql.get(app.dbName).get('imei_list', {
            imei: ctx.request.body.imei
        });
        if (!list) {
            app.mysql.get(app.dbName).insert('imei_list', {
                ...ctx.request.body
            })
        }
        return 'success'
    }
}